<?php
namespace AuctionAppApi\Controller;

use AuctionCore\CarDealer\BuyCarList;
use AuctionCore\System\AuctionException;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\CarResource\CarModel;
use AuctionCore\CarResource\Car;
use AuctionCore\Dispute\DisputeOrder;
/**
 * 车商争议管理接口
 */
class DisputeController extends BaseController
{

    //争议页面信息
    public function get_order_dispute(){
        $order_id = I('post.order_id');
        $status_type = I('post.state_type');
        if ($order_id < 1) {
            AuctionException::make_throw('700027');
        }
        $bl = new BuyCarList();
        $res = $bl->get_dispute($order_id,$status_type);
        if($res == 1){
            AuctionException::make_throw('700046');
        }
        //拍单基本信息
        $o = new AuOrder($order_id);
        $order_info = $o->detail();
        //车辆基本信息
        $car = new Car($order_info['car_id']);
        $car_info = $car->detail();

        //车型
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        $city_name = $a->get_name_by_code($car_info['location_area']);
        $series = $cm->get_model_by_id($car_info['series_id'], 'series');
        $car_model = $city_name.' '.$series['name'];
        $data = array();
        $data['car_model'] = $car_model;
        $data['order_no'] = $order_info['order_no'];
        if($status_type == 3){
            $data['reasons'][0]['name'] = "车况与检测报告不符";
            $data['reasons'][0]['value'] = 1;
            $data['reasons'][0]['selected'] = true;
        }else{
            $data['reasons'][0]['name'] = "车况与检测报告不符";
            $data['reasons'][0]['value'] = 1;
            $data['reasons'][1]['name'] = "过户超时";
            $data['reasons'][1]['value'] = 2;
        }
        $this->api_200($data);
    }
    //添加争议接口
    public function dispute_save(){
        $order_id = I('post.order_id');
        $order_status = I('post.state_type'); //待验车至待过户之间时，每个状态仅能提交一次争议；其他状态不可以提争议。
        $reason = I('post.reason'); //争议原因
        $remark = I('post.remark'); //备注
        $dealer_id = I('post.dealer_id'); //车商id
        if($order_id<1){
            AuctionException::make_throw('700027');
        }
        $bl = new BuyCarList();
        $res = $bl->get_dispute($order_id,$order_status);
        if($res == 1){
            AuctionException::make_throw('700046');
        }
        $data = array();
        //拍单基本信息
        $o = new AuOrder($order_id);
        $order_info = $o->detail();
        if($order_info['is_lock'] == 1){
            AuctionException::make_throw('500074');
        }
        $poInfo = $o->getPurchaseByOrderId();
        //车辆基本信息
        $car = new Car($order_info['car_id']);
        $car_info = $car->detail();
        $car_plate_number = C('PLATE_PREFIX')[$car_info['plate_prefix']] . $car_info['plate_suffix'];
        $data['order_id'] = $order_id;
        $data['remark'] = $remark;
        $data['dispute_reason'] = $reason;
        $data['order_no'] = $order_info['order_no'];
        $data['od_id'] = $dealer_id;
        $data['od_type'] = 1; //1:车商 2:车主
        $data['car_plate_number'] = $car_plate_number;
        $data['po_id'] = $poInfo['id'];
        $data['po_no'] = $poInfo['purchase_order_nu'];
        $data['status'] = 1;
        $data['createtime'] = date('Y-m-d H:i:s');
        $data['order_status'] = $order_status;
        $dispute = new DisputeOrder();
        $res = $dispute->DisputeAdd($data);
        if($res){
            $this->api_200(null);
        }
    }
    //异常车辆列表
    public function modify_list(){
        $page = I('post.page/d', 1);
        $page_size = I('post.page_size/d', 20);
        $dealer_id = I('post.dealer_id');
        $dispute = new DisputeOrder();
        $info = $dispute->get_modify_list($dealer_id,$page,$page_size);
        $modify = $info['list'];
        $total = $info['total'];
        $modify_list = array();
        foreach($modify as $k => $val){
            $modify_list[$k]['order_id'] = $val['order_id'];
            $img = Car::get_pic_by_key($val['car_id'], "p_left_front45");//左前45°图片
            $modify_list[$k]['img'] = $img['pic_url'];
            $modify_list[$k]['car_model'] = $val['model'];
            $transfer_limit = C('TRANSFER_LIMIT.' . $val['transfer_limit']);
            $modify_list[$k]['summary'] = $val['order_no'].'/'.$transfer_limit.'/上门自提' ;
            switch($val['modify_reason']){
                case 1:
                    $modify_list[$k]['reason'] = "检车失误";
                    break;
                case 2:
                    $modify_list[$k]['reason'] = "平台失误";
                    break;
                case 3:
                    $modify_list[$k]['reason'] = "买方违约";
                    break;
                case 4:
                    $modify_list[$k]['reason'] = "卖方违约";
                    break;
                default;
            }
            $modify_list[$k]['result'] = '处理结果:退车';
            $modify_list[$k]['state'] = 1;
        }
        $data = array(
            'currentpage' => $page,
            'pagesize' => $page_size,
            'maxcount' => $total,
            'maxpage' => ceil($total / $page_size),
            'list' => $modify_list
        );
        $this->api_200($data);
    }
    //异常车辆详情
    public function modify_detail(){
        $order_id = I('post.order_id/d');
        $dealer_id = I('post.dealer_id/d');
        $dispute = new DisputeOrder();
        $modify_info = $dispute->get_modify_detail($order_id,$dealer_id);
        //拍单基本信息
        $order_id = $modify_info['order_id'];
        $o = new AuOrder($order_id);
        $order_info = $o->detail();
        $car = array();
        //车辆基本信息
        $c = new Car($order_info['car_id']);
        $car_info = $c->detail();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        $city_name = $a->get_name_by_code($car_info['location_area']);
        $series = $cm->get_model_by_id($car_info['series_id'], 'series');
        $car['name'] = '['.$city_name.'] '.$series['name'] . ' ' . $car_info['car_cc'] . ($car_info['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_info['gearbox']);
        $transfer_limit = C('TRANSFER_LIMIT.' . $car_info['transfer_limit']);
        $car['summary'] = $order_info['order_no'].'/'.$transfer_limit.'/上门自提' ;
        $img = Car::get_pic_by_key($car_info['car_id'], "p_left_front45");//左前45°图片
        $car['img'] = $img['pic_url'];
        $car['time'] = $modify_info['createtime'];
        $car['status'] = "交易关闭";
        $car['status_color'] = 0xffff4835;
        $car['type'] = "car_cover";
        $info = array();
        $info['detail'][] = $car;
        $detail['type'] = "group";
        $list = array();
        $list1['type'] = "name_value";
        $list1['name'] = "原因";
        switch($modify_info['modify_reason']){
            case '1':
                $list1['value'] = "检测失误";
                break;
            case '2':
                $list1['value'] = "平台失误";
                break;
            case '3':
                $list1['value'] = "买方违约";
                break;
            case '4':
                $list1['value'] = "卖方违约";
                break;
            default;
        }
        $detail['list'][] = $list1;
        $list2['type'] = "name_value";
        $list2['name'] = "处理结果";
        $list2['value'] = $modify_info['modify_result'] == 2 ? "订单关闭" : " ";
        $detail['list'][] = $list2;
        $list3['type'] = "name_value";
        $list3['name'] = "已收车款";
        $au = new AuOrder($order_id);
        $collection_id = $au->getCollectionIdByOid();
        $amount = $au->getCaramount($collection_id); //已收金额
        $amount == null ? $list3['value'] = '0' : $list3['value'] = strval($amount);
        $detail['list'][] = $list3;
        $list4['type'] = "name_value";
        $list4['name'] = $modify_info['dealer_compensate'] > 0 ? "赔付金额" : "扣除金额";
        $modify_info['dealer_compensate'] > 0 ? $list4['value'] = $modify_info['dealer_compensate'] : $list4['value'] = $modify_info['dealer_deduction'];
        if($list4['value'] == '0.00'){
            $list4['value'] = 0;
        }
        $detail['list'][] = $list4;
        $list5['type'] = "name_value";
       if($modify_info['dealer_compensate'] > 0){
           $list5['name'] = "应退金额";
           $list5['value'] = strval($au->getCarPayment($modify_info['mid'],4));
       }else if($modify_info['dealer_deduction'] > 0){
            if($amount - $modify_info['dealer_deduction'] > 0){
                $list5['name'] = "应退金额";
                $list5['value'] = strval($au->getCarPayment($modify_info['mid'],4));
            }else{
                $list5['name'] = "应收金额";
                $list5['value'] = strval($au->getCarCollection($modify_info['mid'],6));
            }
       }else if($amount - $modify_info['dealer_deduction'] == 0){
           $list5['name'] = "应退金额";
           $list5['value'] = 0;
       }
        $detail['list'][] = $list5;
        $list6['type'] = "name_value";
        $list6['name'] = "处理状态";
        switch($modify_info['status']){
            case '0':
                $list6['value'] = "待执行";
                    break;
            case '1':
                $list6['value'] = "已执行";
                break;
            case '2':
                $list6['value'] = "已完成";
                break;
            case '3':
                $list6['value'] = "拒绝执行";
                break;
            case '4':
                $list6['value'] = "作废";
                break;
            default;
        }
        $detail['list'][] = $list6;
        $list7['type'] = "name_value";
        $list7['name'] = "发生时间";
        $list7['value'] = $modify_info['createtime'];
        $detail['list'][] = $list7;
        $list8['type'] = "name_value";
        $list8['name'] = "处理完成时间";
        switch($modify_info['status']){ //处理状态
            case '0':
                $list8['value']=  '-';
                break;
            case '1':
                $list8['value']= $modify_info['dealtime'];
                break;
            case '2':
                $list8['value']= $modify_info['completetime'];
                break;
            case '3':
                $list8['value']= $modify_info['refusetime'];
                break;
            case '4':
                $list8['value']= $modify_info['canceltime'];
                break;
            default;
        }
        $detail['list'][] = $list8;
        $list9['type'] = "name_value";
        $list9['name'] = "取消交易说明";
        $list9['value'] = $modify_info['modify_desc'];
        $detail['list'][] = $list9;
        $info['detail'][] = $detail;
        $this->api_200($info);
    }
    //争议车辆列表
    public function dispute_list(){
        $page = I('post.page/d', 1);
        $page_size = I('post.page_size/d', 20);
        $dealer_id = I('post.dealer_id');
        $dispute = new DisputeOrder();
        $info = $dispute->get_dispute_list($dealer_id,$page,$page_size);
        $dispute = $info['list'];
        $total = $info['total'];
        $dispute_list = array();
        foreach($dispute as $k => $val){
            $dispute_list[$k]['order_id'] = $val['order_id'];
            $dispute_list[$k]['did'] = $val['did'];
            $img = Car::get_pic_by_key($val['car_id'], "p_left_front45");//左前45°图片
            $dispute_list[$k]['img'] = $img['pic_url'];
            $dispute_list[$k]['car_model'] = $val['model'];
            $transfer_limit = C('TRANSFER_LIMIT.' . $val['transfer_limit']);
            $dispute_list[$k]['summary'] = $val['order_no'].'/'.$transfer_limit.'/上门自提' ;
            $dispute_list[$k]['create_time'] = $val['createtime'];
        }
        $data = array(
            'currentpage' => $page,
            'pagesize' => $page_size,
            'maxcount' => $total,
            'maxpage' => ceil($total / $page_size),
            'list' => $dispute_list
        );
        $this->api_200($data);
    }
    //争议车辆详情
    public function dispute_detail()
    {
        $dealer_id = I('post.dealer_id');
        $did = I('post.did');
        $dispute = new DisputeOrder();
        $dispute_info = $dispute->get_dispute_detail($did);
       // print_r($dispute_info);die;
        //拍单基本信息
        $order_id = $dispute_info['order_id'];
        $o = new AuOrder($order_id);
        $order_info = $o->detail();
        $detail = array();
        $car = array();
        //车辆基本信息
        $c = new Car($order_info['car_id']);
        $car_info = $c->detail();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        $city_name = $a->get_name_by_code($car_info['location_area']);
        $series = $cm->get_model_by_id($car_info['series_id'], 'series');
        $car['name'] = '[' . $city_name . '] ' . $series['name'] . ' ' . $car_info['car_cc'] . ($car_info['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_info['gearbox']);
        $transfer_limit = C('TRANSFER_LIMIT.' . $car_info['transfer_limit']);
        $car['summary'] = $order_info['order_no'] . '/' . $transfer_limit . '/上门自提';
        $img = Car::get_pic_by_key($car_info['car_id'], "p_left_front45");//左前45°图片
        $car['img'] = $img['pic_url'];
        $car['time'] = $dispute_info['createtime'];
        $car['type'] = "car_cover";
        $info['detail'][] = $car;
        $list1['type'] = "group";
        $list1['title'] = "争议信息";
        $list1['list'][0]['type'] = "name_value";
        $list1['list'][0]['name'] = "争议原因";
        switch ($dispute_info['dispute_reason']) {
            case '1':
                $list1['list'][0]['value'] = "车况不符";
                break;
            case '2':
                $list1['list'][0]['value'] = "过户超时";
                break;
            default;
        }
        $list1['list'][1]['type'] = "name_value";
        $list1['list'][1]['name'] = "争议时间";
        $list1['list'][1]['value'] = $dispute_info['createtime'];
        $info['detail'][] = $list1;
        $dispute_result = $dispute->getmodify_resultbydid($dispute_info['did']);
        if ($dispute_info['status'] == 2){
            $list2['type'] = "group";
            $list2['title'] = "争议结果";
            $list2['list'][0]['type'] = "name_value";
            $list2['list'][0]['name'] = "审核结果";
            $list2['list'][0]['value'] = "通过";
            $list2['list'][1]['type'] = "name_value";
            $list2['list'][1]['name'] = "处理方式";
            $list2['list'][1]['value'] = $dispute_result['modify_result'] == 1 ? "赔付" : "退车";
            $list2['list'][2]['type'] = "name_value";
            $list2['list'][2]['name'] = "赔付金额"; //只有处理结果是2 通过时,才会发生赔付金额
            $au = new AuOrder($order_id);
            $list2['list'][2]['value'] = $dispute_result['dealer_compensate'];
            $info['detail'][] = $list2;
            $list3['type'] = "group";
            if($dispute_result['modify_result'] == 2){
                $list3['list'][0]['type'] = "name_value";
                $list3['list'][0]['name'] = "应退金额";
                $pay_amount = $au->getCarPayment($dispute_result['mid'],4);
                $list3['list'][0]['value'] = strval($pay_amount);
                $list3['list'][0]['value_color'] = 0xffff6f37;
                $list3['list'][1]['type'] = "name_value";
                $list3['list'][1]['name'] = "仲裁时间";
                $dispute_info['audittime'] == '0000-00-00 00:00:00' ? $list3['list'][1]['value'] = '' : $list3['list'][1]['value'] = $dispute_info['audittime'];
            }else{
                $list3['list'][0]['type'] = "name_value";
                $list3['list'][0]['name'] = "仲裁时间";
                $dispute_info['audittime'] == '0000-00-00 00:00:00' ? $list3['list'][0]['value'] = '' : $list3['list'][0]['value'] = $dispute_info['audittime'];
            }
            $info['detail'][] = $list3;
        }else if($dispute_info['status'] == 3){
            $list2['type'] = "group";
            $list2['title'] = "争议结果";
            $list2['list'][0]['type'] = "name_value";
            $list2['list'][0]['name'] = "审核结果";
            $list2['list'][0]['value'] = "驳回";
            $list2['list'][1]['type'] = "name_value";
            $list2['list'][1]['name'] = "仲裁时间";
            $dispute_info['audittime'] == '0000-00-00 00:00:00' ? $list2['list'][1]['value'] = '' : $list2['list'][1]['value'] = $dispute_info['audittime'];
            $info['detail'][] = $list2;
        }else if($dispute_info['status'] == 4) {
            $list2['type'] = "group";
            $list2['title'] = "争议结果";
            $list2['list'][0]['type'] = "name_value";
            $list2['list'][0]['name'] = "审核结果";
            $list2['list'][0]['value'] = "作废";
            $list2['list'][1]['type'] = "name_value";
            $list2['list'][1]['name'] = "仲裁时间";
            $dispute_info['audittime'] == '0000-00-00 00:00:00' ? $list2['list'][1]['value'] = '' : $list2['list'][1]['value'] = $dispute_info['audittime'];
            $info['detail'][] = $list2;
        }
        $list4['type'] = "report";
        $list4['text'] = "检测报告";
        $list4['car_id'] = $car_info['car_id'];
        $info['detail'][] = $list4;
        $this->api_200($info);
    }
}